﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL.CarriersBLL;
using Model;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace MDMUI.Carrier
{
    public partial class Carriers : Form
    {
        private readonly CarriersHome _carriersHome = new CarriersHome();
        public Carriers()
        {
            InitializeComponent();
            //LoadCarrierLists();
            InitializeComboBoxes();
        }

        // 初始化下拉框
        private void InitializeComboBoxes()
        {
            // 初始化 TypecomboBox（详细载具类型）
            TypecomboBox.Items.Add("All");
            TypecomboBox.Items.Add("Magazine");
            TypecomboBox.Items.Add("new");
            TypecomboBox.SelectedIndex = 0;

            //初始化 durableSpecId（耐用品规格号）
            DScomboBox.Items.Add("All");
            DScomboBox.SelectedIndex = 0;

            // 初始化 AScomboBox（容纳状态）
            AScomboBox.Items.Add("All");
            AScomboBox.Items.Add("Full");
            AScomboBox.Items.Add("Empty");
            AScomboBox.SelectedIndex = 0;

            // 初始化 CScomboBox（清洗状态）
            CScomboBox.Items.Add("All");
            CScomboBox.Items.Add("Clean");
            CScomboBox.Items.Add("NotClean");
            CScomboBox.SelectedIndex = 0;

            // 初始化 CS2comboBox（载具状态）
            CS2comboBox.Items.Add("All");
            CS2comboBox.Items.Add("Released");
            CS2comboBox.Items.Add("Created");
            CS2comboBox.SelectedIndex = 0;
        }

        //// 加载载具列表数据到 DataGridView
        //private void LoadCarrierLists()
        //{
        //    try
        //    {
        //        List<CarrierList> carrierLists = _carriersHome.GetAllCarrierLists();
        //        CLdataGridView.DataSource = carrierLists; // 绑定数据源

        //        // 设置 DataGridView 的列标题和宽度（可选）
        //        CLdataGridView.Columns["CarrierListId"].HeaderText = "载具列表ID";
        //        CLdataGridView.Columns["CarrierId"].HeaderText = "载具ID";
        //        CLdataGridView.Columns["DurableSpecId"].HeaderText = "耐用品规格ID";
        //        CLdataGridView.Columns["BatchId"].HeaderText = "批次ID";

        //        CLdataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.DisplayedCells);
        //    }
        //    catch (Exception ex)
        //    {
        //        MessageBox.Show($"加载载具列表失败：{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
        //    }
        //}

        private void selectBtn_Click(object sender, EventArgs e)
        {
            try
            {
                // 获取下拉框和文本框的值
                string detailedCarrierType = TypecomboBox.SelectedItem.ToString();
                string capacityStatus = AScomboBox.SelectedItem.ToString();
                string cleaningStatus = CScomboBox.SelectedItem.ToString();
                string carrierStatus = CS2comboBox.SelectedItem.ToString();
                string carrierId = CarriersIdTxt.Text;

                // 构建查询参数
                List<Dictionary<string, object>> msg = _carriersHome.test(
                    detailedCarrierType: detailedCarrierType == "All" ? null : detailedCarrierType,
                    capacityStatus: capacityStatus == "All" ? null : capacityStatus,
                    cleaningStatus: cleaningStatus == "All" ? null : cleaningStatus,
                    carrierStatus: carrierStatus == "All" ? null : carrierStatus,
                    carrierId: string.IsNullOrEmpty(carrierId) ? null : (int?)int.Parse(carrierId)
                );

                // 将字典列表转换为 DataTable
                DataTable dataTable = new DataTable();
                var desiredColumns = new List<string> { "CarrierId", "CarrierType", "DetailedCarrierType", "DurableSpecId", "CarrierStatus", "CleaningStatus", "LockStatus", "CapacityStatus", "BatchId", "Quantity" , "LocationNumber" , "CarrierCapacity" };
                foreach (var column in desiredColumns)
                {
                    dataTable.Columns.Add(column);
                }

                foreach (var dict in msg)
                {
                    DataRow row = dataTable.NewRow();
                    foreach (var column in desiredColumns)
                    {
                        row[column] = dict.TryGetValue(column, out var value) ? value : DBNull.Value;
                    }
                    dataTable.Rows.Add(row);
                }

                // 将 DataTable 绑定到 DataGridView
                CLdataGridView.DataSource = dataTable;

                // 设置 DataGridView 的列标题和宽度（可选）
                CLdataGridView.Columns["CarrierId"].HeaderText = "载具号";
                CLdataGridView.Columns["DurableSpecId"].HeaderText = "耐用品规格号";
                CLdataGridView.Columns["BatchId"].HeaderText = "批次号";
                CLdataGridView.Columns["CarrierType"].HeaderText = "载具类型";
                CLdataGridView.Columns["DetailedCarrierType"].HeaderText = "载具详细类型";
                CLdataGridView.Columns["CarrierStatus"].HeaderText = "载具状态";
                CLdataGridView.Columns["CleaningStatus"].HeaderText = "清洗状态";
                CLdataGridView.Columns["LockStatus"].HeaderText = "锁定状态";
                CLdataGridView.Columns["CapacityStatus"].HeaderText = "容量状态";
                CLdataGridView.Columns["Quantity"].HeaderText = "批次数量";
                CLdataGridView.Columns["LocationNumber"].HeaderText = "位置号";
                CLdataGridView.Columns["CarrierCapacity"].HeaderText = "载具容量";
            }
            catch (Exception ex)
            {
                MessageBox.Show($"查询失败：{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}